<template>
  <div class="business-handbook">
    <top-header></top-header>

    <div class="search-div">
      <div class="search-one-div">
        <search-one @search="onSearch"></search-one>
      </div>

      <span class="iconfont iconshaixuan"></span>
    </div>

    <div class="common-list-body" v-fullheight>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <a :href="$filePreview(item.fileName, item.storagePath)" class="list-item" v-for="(item, key) in listSource" :key="key">
            <div class="name">{{ item.title }}</div>
            <div class="status">
              <span>业务手册文件：</span>
              <span>{{ item.fileName }}</span>
            </div>
            <div class="status">
              <span>更新时间：</span>
              <span>{{ item.fileUpdateTime }}</span>
            </div>
          </a>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";

@Component({
  name: "BusinessHandbook",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh
  }
})
export default class BusinessHandbook extends Base {
  private listSource: any[] = [];
  private loading = false;
  private finished = false;
  private refreshing = false;

  private apiParams = {
    businessCategoryId: "",
    pageNum: 1,
    pageSize: 20,
    sortBy: "fileUpdateTime", // 排序字段
    order: "desc", // 排序顺序
    searchValue: "" // 搜索内容
  };

  private created() {
    this.apiParams.businessCategoryId = (this.$route.query.businessCategoryId ? this.$route.query.businessCategoryId : "") as string;
  }

  onSearch(event: any) {
    this.apiParams.searchValue = event;
    this.refreshing = true;
    this.onRefresh();
  }

  onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.apiParams.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetBusinessManualList(this.listParams);
      if (res && res.code === 200) {
        if (this.apiParams.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.apiParams.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  /**
   * 预设变量，可能后期扩展可以用到
   */
  get listParams() {
    const _return = this.apiParams;
    return _return;
  }
}
</script>

<style lang="less" scoped>
.business-handbook {
  .search-div {
    background-color: white;
    display: flex;
    align-items: center;
    .iconshaixuan {
      padding: 10px 10px 10px 0px;
      font-size: 22px;
      color: #bfbfbf;
    }
    .search-one-div {
      flex: 1;
    }
  }

  .list-item {
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    display: block;
    padding: 15px;
    position: relative;
    &::before {
      top: 18px;
      left: 0px;
      content: "";
      position: absolute;
      width: 3px;
      height: 14px;
      background: linear-gradient(281deg, #5a96fa -338%, #6dc1fd 93%);
    }
    .name {
      font-size: 16px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      word-break: break-all;
    }
    .status {
      margin-top: 10px;
      display: flex;
      align-content: center;
      color: #717787;
      span:first-child {
        color: #444852;
      }
      span:last-child {
        flex: 1;
      }
    }
    .date {
      color: #a1a6b3;
      margin-top: 8px;
    }
  }
}
</style>
